{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a64bed25",
   "metadata": {},
   "source": [
    "## 0001模块 (import)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0aec1bec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用模块方法： import 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f9335518",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time # 调用时间模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "874ca210",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=11, tm_mday=7, tm_hour=15, tm_min=21, tm_sec=47, tm_wday=0, tm_yday=311, tm_isdst=0)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17536707",
   "metadata": {},
   "source": [
    "* 应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "43305993",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "15\n",
      "41\n",
      "怎么还没到周三呢，哎，等的着急\n"
     ]
    }
   ],
   "source": [
    "# 创建week_day变量,time_hour变量，time_min变量\n",
    "现在周几 = time.localtime().tm_wday # 从模块里找到星期几的数字，赋予变量\n",
    "print(现在周几) # 周一为 0，以此类推周日为 6\n",
    "\n",
    "此刻的小时 = time.localtime().tm_hour\n",
    "print(此刻的小时)\n",
    "\n",
    "此刻的分钟 = time.localtime().tm_min\n",
    "print(此刻的分钟)\n",
    "\n",
    "if 现在周几 == 2: # 如果现在周几为周二\n",
    "    print(\"今天有Python课，好开心呀\")\n",
    "    if 此刻的小时 == 19: # 如果此刻的小时为19时\n",
    "        print(\"Python课ing，刚上课\")\n",
    "    elif 此刻的小时 == 18 and 此刻的分钟 > 44: # elif 意为：否则，如果     如果此刻的小时为18时 并且此刻的分钟大于44分\n",
    "        print(\"Python课ing，现在在第二节课\")\n",
    "    elif 此刻的小时 == 20 and 此刻的分钟 < 51:\n",
    "        print(\"Python课ing,现在是第三节课了\")\n",
    "    elif 此刻的小时< 18:\n",
    "        print(\"期待今天的课\")\n",
    "    else:\n",
    "        print(\"今天的Python课很不错\") # elsa 意为：否则 （以上条件之外的情况下）\n",
    "else:\n",
    "    print(\"怎么还没到周三呢，哎，等的着急\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18deb7ff",
   "metadata": {},
   "source": [
    "## 0002 循环  for"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "859e6cfe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "4\n",
      "6\n",
      "5\n",
      "8\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "# for 语句的基本用法为：\n",
    "# for 变量 in 序列:\n",
    "# 语句1\n",
    "\n",
    "#            elsa 意为：否则 （以上条件之外的情况下）\n",
    "#            elif 意为：否则，如果\n",
    "\n",
    "# （其中，“序列”即为要遍历的列表或者字符串；\n",
    "# “变量”是序列中的元素。\n",
    "# 每得到一个“序列”中的元素，就执行一次“语句1”，\n",
    "# 直到遍历“序列”中所有元素。）\n",
    "这个序列 = [1,2,4,6,5,8,7] # 创建一个序列\n",
    "for 一个变量 in 这个序列:\n",
    "    print(一个变量) # 每次得到这个序列的一个元素，就保存在一个变量中并打印出来，直到遍历所有元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5d8f2d31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n"
     ]
    }
   ],
   "source": [
    "某序列 = \"Hello\"\n",
    "for i in 某序列:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee57a06a",
   "metadata": {},
   "source": [
    "## 0003  range()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7d396c4",
   "metadata": {},
   "source": [
    "> 1, 正向"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "09600a70",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# range(start,stop[,step])\n",
    "# range(开始值，结束值[,步频]) \n",
    "# 不输入开始值默认从0开始；\n",
    "# 只输入一个数代表结束值；结束值按照从零开始，也就是会减一（结束值不可省略）\n",
    "# 步频（步进值）默认为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f6dade28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "for i in  range(1, 4): #代表1到4，不包含4；即是[1,4)\n",
    "    print(i)\n",
    "time.sleep(3) #设置停顿三秒，停三秒进行下一步\n",
    "for i in range(1, 10, 2): #代表1到10，间隔2,不包含10；\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4efa2d6",
   "metadata": {},
   "source": [
    "> 2. 反向"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ded0f064",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "4\n",
      "3\n",
      "2\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "#打印输出10,9,8,7,6,5,4,3,2,1,0\n",
    "for i in range(5, -1, -1):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd1a4c44",
   "metadata": {},
   "source": [
    "## 0004 获取长度  len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c64ef22f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "source": [
    "name = \"freeCodeCamp\"\n",
    "print(len(name)) # 查看字母长度，包括空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3205f320",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "生态环境 = (\"消费者\",\"生产者\",\"分解者\") # 创建一个元组\n",
    "print(len(生态环境)) # 查看的长度就是项数了"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22eca001",
   "metadata": {},
   "source": [
    "## 0005 检查函数对象的类型  type()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "053eea09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'str'>\n",
      "<class 'collections.OrderedDict'>\n",
      "<class '__main__.Data'>\n"
     ]
    }
   ],
   "source": [
    "x = 10\n",
    "print(type(x))\n",
    " \n",
    "s = 'abc'\n",
    "print(type(s))\n",
    " \n",
    "from collections import OrderedDict\n",
    " \n",
    "od = OrderedDict()\n",
    "print(type(od))\n",
    "class Data:\n",
    "    pass\n",
    "d = Data()\n",
    "print(type(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be1577fe",
   "metadata": {},
   "source": [
    "## 0006  随机生成  randdom"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e0813cac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2112481588659164\n",
      "['吼吼']\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# random.random()：生成随机数,0-1之间随机数\n",
    "# random.choices()：随机抽样\n",
    "# random.randint(a,b)：生成随机范围内的整数\n",
    "# random.choice()：从指定序列获取随机元素\n",
    "import random\n",
    "print(random.random())  #生成随机数,0-1之间随机数\n",
    "import random\n",
    "print(random.choices([\"哈哈\",\"嘿嘿\",\"吼吼\"]))  #随机抽样\n",
    "import random\n",
    "print(random.randint(1,7))  #生成随机范围内的整数\n",
    "import random\n",
    "a = [1,3,5,2,7]\n",
    "print(random.choice(a)) #从指定序列获取随机元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30a0cd3c",
   "metadata": {},
   "source": [
    "## 0007 时间函数  time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcd598fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1c9e0d0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "# sleep() 延迟函数\n",
    "time.sleep(2) # 让时间停2秒\n",
    "print(\"hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ca65d75",
   "metadata": {},
   "source": [
    "## 0007 列表(list)  []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "e01a2a8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [\"张杰\",\"丁亮\",\"胡蝶\",\"文姗姗\"] # []这个括号代表列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "d40ed286",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张杰\n",
      "文姗姗\n",
      "胡蝶\n",
      "胡蝶\n"
     ]
    }
   ],
   "source": [
    "print(a[0]) # 访问列表中第一个，也即是0位\n",
    "print(a[-1])  #访问列表中的最后一个值\n",
    "print(a[-2]) #访问列表中的倒数第二个元素，反向索引\n",
    "print(a[len(a)-2]) #效果如上"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adb47568",
   "metadata": {},
   "source": [
    "> 1. 列表元素增加    \n",
    ">>1. .append() 末尾增加\n",
    ">>2. .insert() 指定位置填加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "96d3d63e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [\"张杰\",\"丁亮\",\"胡蝶\",\"文姗姗\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "669a4405",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张杰', '丁亮', '胡蝶', '文姗姗', '李琦']\n"
     ]
    }
   ],
   "source": [
    "a.append(\"李琦\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d6b1108a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张杰', '丁亮', '胡蝶', '朱子豪', '朱子豪', '朱子豪', '文姗姗', '李琦']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.insert(3,\"朱子豪\") # 在第3个位置（算上0）添加\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dee23614",
   "metadata": {},
   "source": [
    "> 2. 列表元素的删除 \n",
    ">>1. .remove() 输入元素删除\n",
    ">>2. .pop() 删除第几位，默认最后一位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e427047e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['丁亮', '胡蝶', '文姗姗']\n"
     ]
    }
   ],
   "source": [
    "a = [\"张杰\",\"丁亮\",\"胡蝶\",\"文姗姗\"]\n",
    "a.remove(\"张杰\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c8eb8a82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正确的删除方式：先查询目标元素是否存在列表中\n",
    "\"丁亮\" in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a12c59cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['丁亮', '胡蝶', '文姗姗']\n"
     ]
    }
   ],
   "source": [
    "a = [\"张杰\",\"丁亮\",\"胡蝶\",\"文姗姗\"]\n",
    "a.pop(0) # 删除第0，也就是第一位\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9579538",
   "metadata": {},
   "source": [
    "## 0008 while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "dccd7757",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯... 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 5\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 6\n"
     ]
    }
   ],
   "source": [
    "# 它可以实现for循环的指定次数\n",
    "count = 1\n",
    "while True:\n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯...\",count)\n",
    "    count = count+1\n",
    "    if count == 7: # 包含0\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "de6fb0e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1加到100的和为5050\n"
     ]
    }
   ],
   "source": [
    "n = 100 # 加到几\n",
    "sum = 0\n",
    "count = 1 # 从几开始加\n",
    "\n",
    "while count <= n:\n",
    "    sum = sum + count\n",
    "    count = count + 1\n",
    "print(\"1加到100的和为%d\" % sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "330a53fa",
   "metadata": {},
   "source": [
    "## 0009  元组(tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e35b5b9",
   "metadata": {},
   "source": [
    "### 元组具有的性质\n",
    "* 有序性\n",
    "* 不可变性（不可增、删、改）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "eacb4a31",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ()代表元组，元组使用圆括号包含元素，而列表使用方括号包含元素。\n",
    "\n",
    "# 元组的创建，只需在圆括号中添加元素并使用逗号分开即可。索引与列表相同，从0开始\n",
    "\n",
    "# 元组中的元素值是不可以修改的\n",
    "\n",
    "# 一般情况下，元组里面存放的是  常量（不可变），何为常量：数字，字符串\n",
    "#                       不存放  变量 如，列表，字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "63c6f4b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "某元组 = (1,2,3,4,5) # 元组的创建\n",
    "for number in 某元组:\n",
    "    print(number)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f748bae9",
   "metadata": {},
   "source": [
    "### 元组与其他函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "d1d4254a",
   "metadata": {},
   "outputs": [],
   "source": [
    "元组 = (3,7,8,2,0,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "dbbe4274",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(元组) # 计算元组元素的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "bb9163da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(元组) # 返回元组中元素的最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "5a947ec5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(元组) # 返回元组中的最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "1f74c13c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 1, 2, 2, 6, 4, 9)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b=[1,1,2,2,6,4,9]\n",
    "tuple(b) # 将列表转化为元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3e70fd7",
   "metadata": {},
   "source": [
    "##  0010  统计出现次数，计数  count()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7336d90",
   "metadata": {},
   "source": [
    "### 基本写法\n",
    "#### str.count(sub, start= 0,end=len(string))\n",
    "* sub -- 搜索的子字符串\n",
    "* start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。\n",
    "* end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "846dda61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# 当前用法：\n",
    "b=[1,1,2,2,6,4,9]\n",
    "print(b.count(1)) # 打印列表b中，1出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "8d003beb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "违纪 = ['张杰', '丁亮', '胡蝶', '朱子豪', '朱子豪', '朱子豪', '文姗姗', '李琦']\n",
    "print(违纪.count(\"朱子豪\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b11f6245",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "717057dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "元组 = (3,7,8,3,2,0,10)\n",
    "print(元组[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "f6653ac3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58a21f62",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
